import { createRouter, createWebHistory } from 'vue-router'
import Router from 'vue-router'
const routes = [
    {//管理员主界面
        path: '/admin',
        name: 'AdminIndex',
        // meta: {
        //     requireAuth: true
        // },
        component: () => import('../views/admin/adminIndex.vue')
    },
    {//团支书主界面
        path: '/manager',
        name: 'ManagerIndex',
        // meta: {
        //     requireAuth: true
        // },
        component: () => import('../views/manager/managerIndex.vue'),
        children:[
            {path: 'menu1-1',
                component: () => import(/* webpackChunkName: "menu1-1" */ '@/views/student/studentIndex.vue'),
                name: 'Menu1-1',
                meta: { title: 'menu1-1' }}
        ]
    },
    {//学生主界面
        path: '/student',
        name: 'StudentIndex',
        // meta: {
        //     requireAuth: true
        // },
        component: () => import('../views/student/studentIndex.vue')
    },
    {//登录界面
        path: '/',
        name: 'Login',
        component: () => import('../views/Login/index.vue')
    },
    {//admin刷新界面
        path: '/f',
        name: 'f',
        component: () => import('../views/f/f.vue')
    },
    {//tzs刷新界面
        path: '/tzs',
        name: 'tzs',
        component: () => import('../views/f/tzs.vue')
    },
    // {//注册界面
    //     path: '/register',
    //     name: 'Register',
    //     component: () => import('../views/Register/register.vue')
    // },
    {//捕获异常url界面
        path: '/:catchAll(.*)',
        name: '/404',
        component: () => import('../views/Notfound/404.vue')
    }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})
// // 解决Vue-Router升级导致的Uncaught(in promise) navigation guard问题
// const originalPush = router.config.globalProperties.push
// Router.config.globalProperties.push = function push (location, onResolve, onReject) {
//     if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
//     return originalPush.call(this, location).catch(err => err)
// }
router.beforeEach((to, from, next) => {
    if (to.meta.requireAuth) {  // 判断该路由是否需要登录权限
        //如果需要就执行下面的代码

        if(sessionStorage.getItem("isLogin")==='1')
        {
            next({path:'/'});
        }
        else if(sessionStorage.getItem("isLogin")==='2')
        {
            // window.location.href = "/manager";
            next({path:'/manager'});
        }
        else if(sessionStorage.getItem("isLogin")==='3')
        {
            next({path:'/admin'});
        }
            // 通过sessionStorage 获取当前的isLogin的值 将我们保存的isLogin的值赋给num,num是顺便取的名称，可以换
            //我们在登录界面，我们使用请求，请求成功后，我们就使用sessionStorage为‘isLogin’保存一个值  1，如果请求失败，就不保存‘isLogin’的值
        //如果请求成功，num的值就是1，请求失败就是null，所以下面进行判断
        else{
            next({
                path: '/',//返回登录界面
                // query: {redirect: to.fullPath}
            })
        }
    }
    else {
        //如果不需要登录权限就直接跳转到该路由
        next();
    }
})


export default router


